using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace KFGame
{

    /// <summary>
    /// 主要实现类
    /// </summary>
    [DisallowMultipleComponent]
    public class KFApplication : MonoBehaviour
    {

        //提示信息
        [HideInInspector]
        public KFMessage kfMessage;


        //UI 框架UI
        [HideInInspector]
        public KFUI ui;


        KF kf;


        /// <summary>
        /// 初始化时被调用
        /// </summary>
        void Awake()
        {
            kf = KF.kf;
        }

        void Start()
        {

            kf.kfevents.addLisener(Events, KFEventManager.TYPE_KFEVENT_DEFAULT);

            loadUI();


        }

        /// <summary>
        /// 事件处理
        /// </summary>
        /// <param name="e"></param>
        private void Events(KFEvent e)
        {
            Debug.Log("KF Application Event:" + e.name);

            switch (e.name)
            {
                case KFEEVENTS.KFE_APPLICATION_EXIT:
                    Exit();
                    break;

                case KFEEVENTS.KFE_SCENE_BACKVIEW_OPEN:
                    if (kf.sceneManager.SceneContains(KFBackViewManager.sceneName))
                        return;
                    StartCoroutine(loadBackView());
                    break;

                case KFEEVENTS.KFE_SCENE_BACKVIEW_CLOSE:
                    KFBackViewManager.destroySceen();
                    kf.sceneManager.visableAll();
                    break;

                case KFEEVENTS.KFE_SCENE_FIRSTSCENE_OPEN:
                    kf.sceneManager.LoadScene(kf.FirstScene);
                    break;

                case KFEEVENTS.KFE_MESSAGE_SHOW:
                    break;

                case KFEEVENTS.KFE_STATISTICS_COINT:
                    StatisticsLog(e.name, e);
                    break;
                case KFEEVENTS.KFE_STATISTICS_CARD:
                    StatisticsLog(e.name, e);
                    break;
                case KFEEVENTS.KFE_STATISTICS_TICKET:
                    StatisticsLog(e.name, e);
                    break;
                case KFEEVENTS.KFE_STATISTICS_ANYTHING:
                    StatisticsLog(e.tag, e);
                    break;
            }


        }


        /// <summary>
        /// 统计数据事件
        /// </summary>
        /// <param name="name"> 储存的名字 </param>
        /// <param name="e"> KFEvent 对象， status =1  valueInt= </param>
        public void StatisticsLog(string name, KFEvent e)
        {
            if (e.status == 0)
                return;

            KFDatasManager datas = new KFDatasManager();
            datas.addData(name, e.valueInt);

        }

        /// <summary>
        /// 加载后台
        /// </summary>
        /// <returns></returns>
        IEnumerator loadBackView()
        {
            yield return null;
            KF.kf.sceneManager.hideAll();
            KFBackViewManager.CreateBackSceen();
        }


        /// <summary>
        /// 加载UI 框架
        /// </summary>
        /// <returns></returns>
        GameObject loadUI()
        {

            GameObject uiobj = new GameObject("KF_UI");
            GameObject tempInstance = Instantiate(Resources.Load("KFUI") as GameObject, KF.kf.gameObject.transform);
            if (tempInstance == null)
                KF.Log("loaded UI Component error Resources KFUI can't be fond");

            ui = tempInstance.GetComponent<KFUI>();



            if (kf.show_FPS)
                ui.FPS.show();

            return tempInstance;

        }

        /// <summary>
        /// 关闭所有消息窗口
        /// </summary>
        public void closeAllMessageBox()
        {
            if (ui == null)
                return;
            ui.closeAllMessage();
        }

        /// <summary>
        ///  关闭并消毁 信息框
        /// </summary>
        /// <param name="name">信息框定义名字</param>
        /// 
        public void closeMessageBox(string name)
        {
            if (ui == null)
                return;
            ui.closeMessage(name);
        }

        /// <summary>
        ///  显示 信息
        /// </summary>
        /// <param name="msg">信息内容</param>
        /// <param name="title">信息标题</param>
        /// <param name="delayed"> 时间控制自动关闭单位秒，默认是0，不会自动关闭。使用 closeMessage（） 手动关闭 </param>
        /// <param name="name">定义信息的名字，相同名字的信息会被复盖</param>
        /// 
        public void showMessageBox(string msg, string title = null, float delayed = 0, string name = "kfmessage")
        {

            if (ui == null)
                return;
            ui.showMessage(msg, title, delayed, name);

        }
        /// <summary>
        ///  显示 信息
        /// </summary>
        /// <param name="msg">信息内容</param>
        /// <param name="title">信息标题</param>
        /// <param name="delayed"> 时间控制自动关闭单位秒，默认是0，不会自动关闭。使用 closeMessage（） 手动关闭 </param>
        /// <param name="name">定义信息的名字，相同名字的信息会被复盖</param>
        /// 
        public void showMessageBoxToCenter(string msg, string title = null, float delayed = 0, string name = "kfmessage")
        {

            if (ui == null)
                return;
            ui.showMessageToCenter(msg, title, delayed, name);

        }

        /// <summary>
        /// 退出游戏
        /// </summary>
        public void Exit()
        {

#if UNITY_EDITOR
            UnityEditor.EditorApplication.isPlaying = false;
#else
            Application.Quit ();
#endif
        }
    }
}
